package com.project.myxima.fragments;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.android.material.textfield.TextInputEditText;
import com.project.myxima.LoginActivity;
import com.project.myxima.R;
import com.project.myxima.utils.LoginFragmentCreator;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class SignUpFragment extends BaseFragment {

    private static final String TAG = "SignUpFragment";
    private TextInputEditText mUserName_et;
    private TextInputEditText mPassword_et;
    private Button mSignup_btn;

    //initView
    @Override
    protected View onSubViewLoaded(LayoutInflater layoutInflater, ViewGroup container) {

        View view = layoutInflater.inflate(R.layout.fragment_sign_up,container,false);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initEvent();
    }

    private void initEvent() {
        mSignup_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signUp();
            }
        });
    }

    private void signUp() {
        String userName = mUserName_et.getText().toString();
        String password = mPassword_et.getText().toString();
        boolean isEmpty = isEmpty(userName,password);
        if(!isEmpty){
            BmobUser user = new BmobUser();
            user.setUsername(userName);
            user.setPassword(password);
            synchronized (this){
                user.signUp(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if(e==null){
                            LoginActivity loginActivity = (LoginActivity) getActivity();
                            assert loginActivity != null;
                            loginActivity.switchFragment(LoginFragmentCreator.SING_IN_FRAGMENT);
                            Toast.makeText(getContext(), "Successfully sign up !", Toast.LENGTH_SHORT).show();

                        }else {
                            if(e.getErrorCode() == 202){
                                Toast.makeText(getContext(), "该用户名已被注册", Toast.LENGTH_SHORT).show();
                                mUserName_et.setText("");
                            }
                        }
                    }
                });
            }
        }else{
            Toast.makeText(getContext(), "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    private boolean isEmpty(String... strings) {
        for (String string : strings) {
            if (TextUtils.isEmpty(string)){
                return true;
            }
        }
        return false;
    }

    private void initView(View view) {
        //find view
        mUserName_et = view.findViewById(R.id.sign_up_user_et);
        mPassword_et = view.findViewById(R.id.sign_up_password_et);
        mSignup_btn = view.findViewById(R.id.sign_up_signUpBt);
    }


}
